var app = getApp()
let upload = ''
import { showTi } from "../../utils/modal.js";
const date = new Date();
const conf = {
  data: {
    hasEmptyGrid: false,
    selectTime:null,
    orderDetailState:null,
    datas:null,
    dayNow:null,
    showCalen:"",
    dataList:[]
  },
  onLoad(options) {
    let orderDetailStateObj = JSON.parse(options.orderDetailState)
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    this.calculateEmptyGrids(cur_year, cur_month);
    this.calculateDays(cur_year, cur_month);
    this.selected()
    this.setData({
      cur_year,
      cur_month,
      weeks_ch,  
      orderDetailState: orderDetailStateObj
    })
    console.log(this.data.orderDetailState)
    upload = getCurrentPages()[2]
  },
  onUnload: function () {
    upload.orderDetailState()
  },
  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate();
  },
  getFirstDayOfWeek(year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  },
  calculateEmptyGrids(year, month) {
    const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    let empytGrids = [];
    if (firstDayOfWeek > 0) {
      for (let i = 0; i < firstDayOfWeek; i++) {
        empytGrids.push(i);
      }
      this.setData({
        hasEmptyGrid: true,
        empytGrids
      });
    } else {
      this.setData({
        hasEmptyGrid: false,
        empytGrids: []
      });
    }
  },
  calculateDays(year, month) {
    let days = [];
    let calenObj
    const cur_data = date.getDate();
    const thisMonthDays = this.getThisMonthDays(year, month);
    for (let i = 1; i <= thisMonthDays; i++) {
      calenObj={}
      if (cur_data==i){
        calenObj.id = i
        this.setData({
          showCalen:i
        })
      }
      calenObj.day=i
      days.push(calenObj);
    }
    this.setData({
      dataList:days
    });
    console.log(this.data.showCalen)
  },
  handleCalendar(e) {
    const handle = e.currentTarget.dataset.handle;
    const cur_year = this.data.cur_year;
    const cur_month = this.data.cur_month;
    if (handle === 'prev') {
      let newMonth = cur_month - 1;
      let newYear = cur_year;
      if (newMonth < 1) {
        newYear = cur_year - 1;
        newMonth = 12;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);

      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })

    } else {
      let newMonth = cur_month + 1;
      let newYear = cur_year;
      if (newMonth > 12) {
        newYear = cur_year + 1;
        newMonth = 1;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);

      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })
    }
  },
  selected(e){
    console.log(e)
    if(e){
      console.log(this.data.dataList)
      this.setData({
        dayNow: e.currentTarget.dataset.idx,
        datas: this.data.cur_year + "-" + this.changeData(this.data.cur_month) + "-" + this.changeData(e.currentTarget.dataset.idx)+" "+"12:00:00",
        selectTime: (+new Date(this.data.datas)),
        showCalen: e.currentTarget.dataset.idx,
      })
    } 
    console.log(this.data.datas)
    console.log(this.data.selectTime)  
  },
  //日期
  changeData:function(num){
    console.log(num)
    let numpar;
    if (parseInt(num)<10){
      numpar="0"+num
    }else{
      numpar = num
    }
    return numpar
  },
  //提交安装
  saveCard:function(){
    let self=this
    // var timestamp1 = new Date(this.data.datas)
    // var timestamp2 = new Date()
    // var d = timestamp1.getTime() - timestamp2.getTime()
    // if(d<0){
    //   showTi("对不起，安装时间不能小于今天")
    // }else{
      let obj = {
        id: self.data.orderDetailState.id,
        name: self.data.orderDetailState.name,
        tel: self.data.orderDetailState.tel,
        facilitator_id: self.data.orderDetailState.facilitator_id,
        install_date: self.data.selectTime / 1000
      }
      console.log(obj)
      app.query('web/orders/' + self.data.orderDetailState.id, obj, 'put').then(res => {
        if (res.code === 200) {
          console.log(res)
          wx.showModal({
            title: '提示',
            content: '指派成功',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                app.goBack()
              }
            }
          }) 
        }else{
          showTi(res.msg)
        }
      })
    // }
  }
};

Page(conf);